//
//  RootViewController.m
//  sleepTest2
//
//  Created by apple on 2017/8/5.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "RootViewController.h"
#import "DefineHeader.h"
#import "ReportViewController.h"
#import "StartSleepViewController.h"
#import "InfoViewController.h"
// 设置tabBar高度，iPhone6之前的事49，6是96
CGFloat const tabViewHeight = 65;
// 设置标签栏按钮的宽度和高度
CGFloat const btnWidth = 73;
CGFloat const btnHeight = 55.89;

@interface RootViewController ()

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 隐藏系统默认的样式
//    self.tabBar.hidden = YES;
    
    // 调用初始化视图控制器
    [self initViewController];
    
    // 调用初始化标签栏
//    [self initTabBarView];
}

// 初始化视图控制器
- (void)initViewController{
    // 用storyboard初始化 报告 视图控制器
    UIStoryboard *ReportSB = [UIStoryboard storyboardWithName:@"Report" bundle:nil];
    UINavigationController *ReportVC = (UINavigationController *)ReportSB.instantiateInitialViewController;
    // 使用代理
    ReportVC.delegate = self;
    
    UITabBarItem *ReportItem = [[UITabBarItem alloc] initWithTitle:@"报告" image:[UIImage imageNamed:@"Report"] tag:0];
    ReportVC.tabBarItem = ReportItem;
    
    // 用storyboard初始化 开始睡觉 视图控制器
    UIStoryboard *StartSleepSB = [UIStoryboard storyboardWithName:@"StartSleep" bundle:nil];
    UINavigationController *StartSleepVC = (UINavigationController *)StartSleepSB.instantiateInitialViewController;
    StartSleepVC.delegate = self;
    
    UITabBarItem *StartSleepItem = [[UITabBarItem alloc] initWithTitle:@"开始睡觉" image:[UIImage imageNamed:@"startSleep-tabBar"] tag:1];
    StartSleepVC.tabBarItem = StartSleepItem;
    
    // 用storyboard初始化 信息中心 视图控制器
    UIStoryboard *InfoSB = [UIStoryboard storyboardWithName:@"Info" bundle:nil];
    UINavigationController *InfoVC = (UINavigationController *)InfoSB.instantiateInitialViewController;
    InfoVC.delegate = self;
    
    UITabBarItem *InfoItem = [[UITabBarItem alloc] initWithTitle:@"信息中心" image:[UIImage imageNamed:@"Info"] tag:2];
    InfoVC.tabBarItem = InfoItem;
    
    
    // 数组用于存放视图控制器
    NSArray *vcArray = @[ReportVC,StartSleepVC,InfoVC];
    // 可变数组，用于存放标签控制器控制的导航控制器
//    NSMutableArray *tabArray = [NSMutableArray arrayWithCapacity:vcArray.count];
    // 初始化导航控制器
//    for (int i = 0; i < vcArray.count; i++) {
//        UINavigationController *navCtrl = [[UINavigationController alloc]initWithRootViewController:vcArray[i]];
//        [tabArray addObject:navCtrl];
//    }
    // 将导航控制器给标签控制器
    self.viewControllers = vcArray;
    
}

// 自定义标签工具栏
//- (void)initTabBarView{
//    // 初始化标签工具栏视图
//    _tabBarView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight - tabViewHeight, kScreenWidth, tabViewHeight)];
//    _tabBarView.backgroundColor = titleBlueColor;
//    [self.view addSubview:_tabBarView];
//    // 新语法创建数组，拿到图片
//    NSArray *imgArray = @[@"Report",@"startSleep-tabBar",@"Info"];
//    // 创建按钮
//    for (int i = 0; i < imgArray.count; i++) {
//        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
//        [btn setBackgroundImage:[UIImage imageNamed:imgArray[i]] forState:UIControlStateNormal];
//        btn.frame = CGRectMake(btnWidth * i, (tabViewHeight - btnHeight)/2, btnWidth, btnHeight);
//        // 为按钮设置tag，iOS中100以内的tag值有特殊用法，需跳过
//        btn.tag = 100 + i;
//        [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
//        [self.tabBarView addSubview:btn];
//    }
//}

// 标签栏按钮点击事件
//- (void)btnAction:(UIButton *)button{
//    // 根据tag值判断当前索引
//    self.selectedIndex = button.tag - 100;
//}

// 实现showTabBar:方法(是否显示工具栏）
- (void)showTabBar:(BOOL)show{
    CGRect frame = self.tabBarView.frame;
    if (show) {
        frame.origin.x = 0;
    }else{
        frame.origin.x = - kScreenWidth;
    }
    // 重新赋值frame
    [UIView animateWithDuration:0.2 animations:^{
        self.tabBarView.frame = frame;
    }completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
